const tool = require('../tools/tool')

module.exports = {
    import: {
        exp: '导入试卷',
        auth: {
            admin: true
        },
        method: 'post',
        async func(data) {
            await tool.methods.paper().import(data.param.categoryId, data.param.paperName, data.param.list, data.param.isEassy)
        }
    },
    getQuestionList: {
        exp: '获取试卷问题列表',
        auth: true,
        method: 'get',
        async func(data) {
            return await tool.methods.question().getList(parseInt(data.query.paperId))
        }
    },
    updateQuestionList: {
        exp: '更新试卷问题列表',
        auth: {
            admin: true
        },
        method: 'post',
        async func(data) {
            let fileContent = await tool.readFileContent(data.param.fileName)
            if (fileContent === null) throw '文件不存在'
            // tool.deleteFile(data.param.fileName)
            try {
                fileContent = tool.JSON.parse(fileContent)
            } catch (error) {

            }
            let list = fileContent
            for (let i = 0; i < list.length; i++) {
                if (list[i].new) {
                    await tool.methods.question().add(data.param.categoryId, data.param.paperId, list[i])
                } else {
                    await tool.methods.question().update(list[i].id, list[i])
                }
            }
        }
    },
    delete: {
        exp: '删除试卷',
        method: 'post',
        auth: {
            admin: true
        },
        async func(data) {
            await tool.methods.paper().delete(data.param.paperId)
        }
    }
}